var t = require("../../@babel/runtime/helpers/interopRequireDefault").default, e = require("../../config/Links"), n = t(require("../../api/api_shareCard")), a = t(require("../../utils/Tips")), o = t(require("../../kta/index")), i = 0, s = getApp();

o.default.Page({
    data: {
        unionid: "",
        poster: "",
        windowWidth: "",
        windowHeight: "",
        bgImg: e.imageBaseUrl.shareCard + "bg_new.png",
        qrcode: "",
        qrcodeFromInterNet: ""
    },
    onLoad: function(t) {
        this.init();
    },
    init: function() {
        var t = this;
        this.getTempImage(this.data.bgImg).then(function(e) {
            t.setData({
                bgImg: e
            }), s.globalData.aucMiniAuth.sid ? t.getWindowWidth().then(function() {
                return t.getImgUrl();
            }).then(function(e) {
                return t.getTempImage(e).then(t.drawImage);
            }).catch(function() {
                wx.hideLoading();
            }) : (a.default.toast("缺少sid,重新获取"), t.getMiniAuth().then(function() {
                return t.getWindowWidth();
            }).then(function() {
                return t.getImgUrl();
            }).then(function(e) {
                return t.getTempImage(e).then(t.drawImage);
            }).catch(function() {
                wx.hideLoading();
            }));
        });
    },
    getMiniAuth: function() {
        return new Promise(function(t, e) {
            n.default.getQQ().then(function() {
                t();
            }).catch(function() {
                e();
            });
        });
    },
    getImgUrl: function() {
        return new Promise(function(t, e) {
            var n = "";
            s.globalData.aucMiniAuth.sid ? n = "?page=".concat(encodeURI("pages/shareCard_newUser/index"), "&sid=").concat(s.globalData.aucMiniAuth.sid, "&channel=").concat(s.globalData.aucChannel) : (a.default.toast("缺少分享信息sid"), 
            e());
            var o = s.globalData.baseUrl + "/actops-client/v0/invite/miniPageCode" + n;
            console.log("qrImgUrl", o), t(o);
        });
    },
    getTempImage: function(t) {
        var e = this;
        return t ? new Promise(function(n, o) {
            var i = e;
            wx.getImageInfo({
                src: t,
                success: function(e) {
                    console.log("getImageInfo", e), e.path && e.width > 0 && e.height > 0 ? (i.setData({
                        qrcodeFromInterNet: t,
                        qrcode: e.path
                    }), n(e.path)) : (wx.showModal({
                        content: "未获取到二维码图片",
                        showCancel: !1
                    }), o());
                },
                fail: function(e) {
                    console.log("getImageInfo", t, e), a.default.toast("获取图片临时路径失败"), o(e);
                }
            });
        }) : (wx.showModal({
            content: "未获得分享二维码，请刷新重试",
            showCancel: !1
        }), !1);
    },
    getWindowWidth: function() {
        wx.showLoading({
            title: "加载中"
        });
        var t = this;
        return new Promise(function(e, n) {
            wx.getSystemInfo({
                success: function(n) {
                    t.setData({
                        windowWidth: n.windowWidth,
                        windowHeight: n.windowHeight
                    }), e();
                },
                fail: function() {
                    a.default.toast("获取系统信息失败"), n();
                }
            });
        });
    },
    drawImage: function(t) {
        var e = this, n = wx.createCanvasContext("canvasPoster", this), a = this.data.windowWidth, o = 1.7 * this.data.windowWidth, s = .08 * this.data.windowWidth, c = 1.248 * this.data.windowWidth;
        n.drawImage(this.data.bgImg, 0, 0, a, o), n.save(), n.drawImage(this.data.qrcode, s, c, 112, 112), 
        n.save(), n.draw(), setTimeout(function() {
            wx.canvasToTempFilePath({
                canvasId: "canvasPoster",
                x: 0,
                y: 0,
                destWidth: 2 * a,
                destHeight: 2 * o,
                success: function(t) {
                    console.log("canvasToTempFilePath", t), e.setData({
                        poster: t.tempFilePath
                    });
                },
                fail: function(t) {
                    e.setData({
                        poster: ""
                    }), (i += 1) < 3 && e.drawImage(), console.log("canvasToTempFilePath", t);
                },
                complete: function() {
                    wx.hideLoading();
                }
            }, e);
        }, 200);
    },
    savePoster: function() {
        var t = this;
        this.data.poster ? wx.saveImageToPhotosAlbum({
            filePath: this.data.poster,
            success: function(t) {
                wx.showToast({
                    title: "分享画报已保存，快去分享给好友吧。",
                    icon: "none"
                });
            },
            fail: function() {
                wx.showModal({
                    content: "保存失败，请授权允许使用我的相册",
                    showCancel: !1,
                    success: function() {
                        wx.openSetting({
                            success: function(e) {
                                e.authSetting["scope.writePhotosAlbum"] ? t.savePoster() : wx.showModal({
                                    content: "您没有打开保存相册功能，不能保存图片哦~点击右上角设置允许访问相册",
                                    showCancel: !1
                                });
                            },
                            fail: function() {
                                a.default.toast("打开设置失败");
                            }
                        });
                    }
                });
            }
        }) : a.default.toast("无图片，请截图");
    },
    onReady: function() {
        wx.hideShareMenu();
    },
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {
        wx.stopPullDownRefresh(), this.onLoad();
    },
    onReachBottom: function() {},
    onShareAppMessage: function() {}
});